//
//  LocalFileManage.m
//  DownLoadFileExample
//
//  Created by Lin Eidan on 13-2-26.
//  Copyright (c) 2013年 taomee. All rights reserved.
//

#import "LocalFileManage.h"

#include "sys/stat.h"

@implementation LocalFileManage

//获得documents的子文件夹路径
+(NSString *)getPathInsideDocumentsFolder:(NSString *)subFolderName
{
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    return [docPath stringByAppendingPathComponent:subFolderName];
}

+(NSString *)getPathInsideLibraryCachesFolder:(NSString *)subFolderName
{
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    return [docPath stringByAppendingPathComponent:subFolderName];
}


+(BOOL)isExistFile:(NSString *)fileName  //也可以用来判断是否存在文件夹
{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:fileName];
}

+(void)createFolder:(NSString *)fileFolderPath{
    if (![self isExistFile:fileFolderPath]) {
        NSFileManager *fileManager=[NSFileManager defaultManager];
        [fileManager createDirectoryAtPath:fileFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

+(float )bytesTransformToM:(float )size{
    float newSize=size/1024.f/1024.f;
    return newSize;
}

+(float )bytesTransformToG:(float )size{
    float newSize=size/1024.f/1024.f/1024.f;
    return newSize;
}

+(void)deleteFile:(NSString *)filePath{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSError *error; 
    if ([self isExistFile:filePath]) {
        [fileManager removeItemAtPath:filePath error:&error];
    }
}


+ (float)freeDiskSpace{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSError *error;
    NSDictionary *fattributes = [fileManager attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
    NSNumber *temp= [fattributes objectForKey:NSFileSystemFreeSize];
    return [self bytesTransformToG:[temp floatValue]];
}

+ (float)totalDiskSpace{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSError *error;
    NSDictionary *fattributes = [fileManager attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
    NSNumber *temp= [fattributes objectForKey:NSFileSystemSize];
    return [self bytesTransformToG:[temp floatValue]];
}

//+(float)hasUsedDiskSpaceForFolder:(NSString *)folder{
//    long long temp= [self folderSizeAtPath:[self getPathInsideDocumentsFolder:folder]];
//    return [self bytesTransformToM:temp];
//}

// 循环调用fileSizeAtPath来获取一个目录所占空间大小
+ (long long) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize;
}
//获取文件大小,C写的，效率高
+ (long long) fileSizeAtPath:(NSString*) filePath{
    struct stat st;
    if(lstat([filePath cStringUsingEncoding:NSUTF8StringEncoding], &st) == 0){
        return st.st_size;
    }
    return 0;
}

+ (float) getProgress:(NSInteger )totalSize andCurrentSize:(NSInteger )currentSize{
    float pro=(float)currentSize/(float)totalSize;
    return pro;
}

@end
